package io.wongxd.demo.page.views

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.wongxd.demo.M
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.compose.custom.views.refresh_layout.RefreshContentStateEnum
import io.wongxd.solution.compose.custom.views.refresh_layout.VerticalRefreshableLayout
import io.wongxd.solution.compose.custom.views.refresh_layout.rememberRefreshLayoutState
import io.wongxd.solution.compose.custom.views.util.rememberMutableStateOf
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class FgtViewMain : BaseComposeToolBarFgt() {
    override fun lazyInit() {

    }

    override val title: String
        get() = "FgtViewMain  LazyColumn基础功能"

    @Composable
    override fun Render() {
        ComposeContent()
    }

    private var array = ArrayList(IntArray(20) { it * 2 }.asList())

    data class Value(val a: String, val b: String)


    @Composable
    private fun ComposeContent() {
        val list = remember {
            val list = mutableStateListOf<Int>()
            list.addAll(array)
            list
        }
        var bean by rememberMutableStateOf(value = Value("0", ""))
        Column {
            Divider()
            println("${bean.a.length}")
            Text(
                text = "不动的text${bean.a}  b=${bean.b}",
                M.clickable { bean = bean.copy(bean.a + "0", bean.b + "1") }
                    .verticalScroll(ScrollState(0))
            )
            ShowRv(list, {
                list.addAll(it)
            }, {
                list.clear()
                list.addAll(array)
            })
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    private fun ShowRv(
        list: MutableList<Int>,
        listChangeListener: (List<Int>) -> Unit,
        reSetDataListener: () -> Unit,
    ) {
        val topRefreshLayoutState = rememberRefreshLayoutState(onRefreshListener = {
            mainScope.launch {
                delay(1500)
                "刷新完成".showToast()
                setRefreshState(RefreshContentStateEnum.Stop)
                reSetDataListener()
            }
        })
        val bottomRefreshLayoutState = rememberRefreshLayoutState(onRefreshListener = {
            mainScope.launch {
                delay(1500)
                "加载完成".showToast()
                setRefreshState(RefreshContentStateEnum.Stop)
                listChangeListener(IntArray(20) {
                    Random.nextInt()
                }.asList())
            }
        })
        VerticalRefreshableLayout(
            topRefreshLayoutState = topRefreshLayoutState,
            bottomRefreshLayoutState = bottomRefreshLayoutState,
            content = {
                LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp),
                    content = {
                        stickyHeader {
                            Text(text = "粘懈标题")
                        }
                        item {
                            Text(text = "头布局")
                        }
                        stickyHeader {
                            Text(text = "粘懈标题2")
                        }
                        items(list) {
                            Text(
                                text = it.toString(),
                                M.fillMaxWidth(),
                                textAlign = TextAlign.Center,
                            )
                        }
                        item {
                            Text(text = "尾布局,list.size=${list.size}")
                        }
                        /*item {
                            //骚操作实现上拉加载,不保证没有问题,原理:滑动到最底部,就会触发最底部的这个item函数,然后执行副作用函数
                            //加载的布局
                            Text(text = "加载中")
                            if (job == null) {
                                job = mainScope.launch {
                                    try {
                                        delay(1000)
                                        Toast.makeText(this@MainListActivity, "加载完成", Toast.LENGTH_LONG)
                                            .show()
                                        listChangeListener(IntArray(20) {
                                            random.nextInt()
                                        }.asList())
                                    } finally {
                                        job = null
                                    }
                                }
                            }
                        }*/
                    })
            })
    }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}